//
//  ViewController.m
//  15-collectionViewLifeCycle
//
//  Created by 王道林 on 16/8/3.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 15) / 2, 120);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"wang"];
    
    
    //关于collectionview lifeCycle
    //1.numberOfsection
    //2.collectionview:numberOfitemInsection
    //3.prepareforreuse(从重用池取出时调用)
    //4.collectionview cellforitemAtIndexPath
    //5.collectionview:willdisplaycell
    //注意:3-5循环执行
    //当cell完全离开屏幕时
    //6.collectionview:didEnddisplay
    
    //iOS 10以前
    //出屏幕立即返回,依旧进入重用池3-4-5
    //iOS10以后 出屏幕会执行4-5 不会执行3 提高了效率
    
    
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 100;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"wang" forIndexPath:indexPath];
    cell.backgroundColor = COLORAUTO;
    return cell;
}



- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@----%ld----[%@:%s]", cell, indexPath.row, self, __FUNCTION__);
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@:%s]",self, __FUNCTION__);
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
